/* libfake437 - a library for the rendering of code page 437 `graphics'
 * Copyright (C) 2008, 2009 Jack Kelly <endgame.dos@gmail.com>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <SDL.h>
#include "fake437/types.h"
#include "fake437/primitive.h"
#include "fake437/surface.h"

F437Surface* f437_surface_new(SDL_Surface* surface,
                              const F437Font* font) {
  F437Surface* result = f437_surface_try_new(surface, font);
  if(result == NULL) abort();
  return result;
}

F437Surface* f437_surface_try_new(SDL_Surface* surface,
                                  const F437Font* font) {
  F437Surface* result = malloc(sizeof(*result));
  if (result == NULL) return NULL;
  result->surface = surface;
  result->font = font;
  result->left = (surface->w % font->char_width) / 2;
  result->top = (surface->h % font->char_height) / 2;
  result->width = surface->w / font->char_width;
  result->height = surface->h / font->char_height;
  return result;
}

void f437_surface_free(F437Surface* surface) {
  free(surface);
}

void f437_surface_put_char(F437Surface* surface, int left, int top, int ch,
                           const SDL_Color* fg, const SDL_Color* bg) {
  assert(surface != NULL);
  assert(surface->surface != NULL);
  assert(left < surface->width);
  assert(top < surface->height);
  f437_put_char(surface->surface, surface->font,
                surface->left + surface->font->char_width * left,
                surface->top + surface->font->char_height * top,
                ch, fg, bg);
}

void f437_surface_put_rect(F437Surface* surface, int left, int top,
                           int width, int height, int ch,
                           const SDL_Color* fg, const SDL_Color* bg) {
  assert(surface != NULL);
  assert(surface->surface != NULL);
  assert(width >= 0);
  assert(height >= 0);
  assert(left + width <= surface->width);
  assert(top + height <= surface->height);
  f437_put_rect(surface->surface, surface->font,
                surface->left + surface->font->char_width * left,
                surface->top + surface->font->char_height * top,
                width, height, ch, fg, bg);
}

void f437_surface_put_string(F437Surface *surface, int left, int top,
                             const char *string,
                             const SDL_Color* fg, const SDL_Color* bg) {
  assert(surface != NULL);
  assert(surface->surface != NULL);
  assert(left >= 0);
  assert(top >= 0);
  assert(top < surface->width);
  f437_put_string(surface->surface, surface->font,
                  surface->left + surface->font->char_width * left,
                  surface->top + surface->font->char_height * top,
                  string, fg, bg);
}
